package com.cy.src.common.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;


import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * 基础Activity
 *
 * @author CY
 * @since 2016/5/12 15:19
 * email tmxdyf@163.com
 */
public class CommonActivity extends AppCompatActivity implements View.OnClickListener {

    private CompositeSubscription mCompositeSubscription;

//    private LoadingDialog mLoadingDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeAllSubscription();
    }

    @Override
    public void onClick(View v) {
    }

//    protected void showLoadingDialog() {
//        mLoadingDialog = LoadingDialog.showLoadDialog(this);
//    }
//
//    protected void hideLoadingDialog() {
//        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
//            mLoadingDialog.dismiss();
//            mLoadingDialog = null;
//        }
//    }

    protected void addSubscription(Subscription subscription) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(subscription);
    }

    protected void removeAllSubscription() {
        if (mCompositeSubscription != null && !mCompositeSubscription.isUnsubscribed()) {
            mCompositeSubscription.unsubscribe();
            mCompositeSubscription.clear();
        }
    }

    protected CompositeSubscription getCompositeSubscription() {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        return mCompositeSubscription;
    }

    protected Snackbar showSnackBar(View view, int textRes) {
        return showSnackBar(view, textRes, Snackbar.LENGTH_SHORT);
    }

    protected Snackbar showSnackBar(View view, int textRes, int duration) {
        hideSoftInputFromWindow(view);
        Snackbar snackbar = Snackbar.make(view, textRes, duration);
        snackbar.show();
        return snackbar;
    }

    protected Snackbar showSnackBar(View view, String text) {
        return showSnackBar(view, text, Snackbar.LENGTH_SHORT);
    }

    protected Snackbar showSnackBar(View view, String text, int duration) {
        hideSoftInputFromWindow(view);
        Snackbar snackbar = Snackbar.make(view, text, duration);
        snackbar.show();
        return snackbar;
    }


    /**
     * 隐藏键盘
     *
     * @param view Token
     */
    protected void hideSoftInputFromWindow(View view) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
